import { useState, useEffect } from 'react';
import { Minus, X, Square, CheckSquare } from 'lucide-react';
import { getCurrentWindow } from '@tauri-apps/api/window';

interface WindowControlsProps {
  onMinimize?: () => void;
  onMaximize?: () => void;
  onClose?: () => void;
  className?: string;
}

const WindowControls: React.FC<WindowControlsProps> = ({
  onMinimize,
  onMaximize,
  onClose,
  className = '',
}) => {
  const [isMaximized, setIsMaximized] = useState(false);

  useEffect(() => {
    const checkMaximized = async () => {
      try {
        const window = getCurrentWindow();
        const isMaximized = await window.isMaximized();
        setIsMaximized(isMaximized);
      } catch (error) {
        console.error('Failed to check window state:', error);
      }
    };

    checkMaximized();
  }, []);

  const handleMinimize = async () => {
    try {
      const window = getCurrentWindow();
      await window.minimize();
      onMinimize?.();
    } catch (error: any) {
      console.error('Failed to minimize window:', error);
      // 如果权限不足，尝试使用不同的方法
      if (error?.message?.includes('Permissions') || error?.message?.includes('allow-minimize')) {
        console.warn('Window minimize permission not available. Using alternative method.');
        // 可以在这里添加备用方法
      }
    }
  };

  const handleMaximize = async () => {
    try {
      const window = getCurrentWindow();
      if (isMaximized) {
        await window.unmaximize();
        setIsMaximized(false);
      } else {
        await window.maximize();
        setIsMaximized(true);
      }
      onMaximize?.();
    } catch (error: any) {
      console.error('Failed to toggle maximize window:', error);
      // 如果权限不足，尝试使用不同的方法
      if (error?.message?.includes('Permissions') || error?.message?.includes('allow-maximize')) {
        console.warn('Window maximize permission not available. Using alternative method.');
        // 可以在这里添加备用方法
      }
    }
  };

  const handleClose = async () => {
    try {
      const window = getCurrentWindow();
      await window.close();
      onClose?.();
    } catch (error: any) {
      console.error('Failed to close window:', error);
      // 如果权限不足，尝试使用不同的方法
      if (error?.message?.includes('Permissions') || error?.message?.includes('allow-close')) {
        console.warn('Window close permission not available. Using alternative method.');
        // 可以在这里添加备用方法
      }
    }
  };

  return (
    <div className={`flex items-center space-x-0 ${className}`}>
      {/* 最小化按钮 */}
      <button
        onClick={handleMinimize}
        className='w-10 h-8 flex items-center justify-center hover:bg-gray-700/50 transition-colors text-gray-300 hover:text-white'
        title='最小化'
      >
        <Minus className='w-6 h-6' />
      </button>

      {/* 最大化/还原按钮 */}
      <button
        onClick={handleMaximize}
        className='w-10 h-8 flex items-center justify-center hover:bg-gray-700/50 transition-colors text-gray-300 hover:text-white'
        title={isMaximized ? '还原' : '最大化'}
      >
        {isMaximized ? <CheckSquare className='w-6 h-6' /> : <Square className='w-6 h-6' />}
      </button>

      {/* 关闭按钮 */}
      <button
        onClick={handleClose}
        className='w-10 h-8 flex items-center justify-center hover:bg-red-500/80 hover:text-white transition-colors text-gray-300'
        title='关闭'
      >
        <X className='w-6 h-6' />
      </button>
    </div>
  );
};

export default WindowControls;
